package com.ruoyi.boat.tcp.client;


import com.ruoyi.boat.service.*;
import com.ruoyi.boat.ws.WebsocketService;
import lombok.Data;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextClosedEvent;
import org.springframework.stereotype.Component;

@Component
@Data
public class TcpClientStarter implements ApplicationRunner, ApplicationListener<ContextClosedEvent> {

    @Value("${netty.server.host}")
    private String host;
    @Value("${netty.client.bd2port}")
    private int bd2port;
    @Value("${netty.client.bd3port}")
    private int bd3port;
    @Value("${netty.client.bd2No}")
    private String bd2No;
    @Value("${netty.client.bd3No}")
    private String bd3No;
    private NettyClient clientBD2;
    private NettyClient clientBD3;

    @Autowired
    private IBoatService boatService;
    @Autowired
    private IPathService pathService;
    @Autowired
    private IStatusService statusService;
    @Autowired
    private IMeteorDataService meteorDataService;
    @Autowired
    private IAisDataService aisDataService;
    @Autowired
    private IAtmosDataService atmosDataService;
    @Autowired
    private IRadarDataService radarDataService;
    @Autowired
    private IWaveDataService waveDataService;
    @Autowired
    private IAdcpDataService adcpDataService;
    @Autowired
    private ICtdDataService ctdDataService;
    @Autowired
    private IWaterQualityService waterQualityService;
    @Autowired
    private WebsocketService websocketService;
    @Override
    public void run(ApplicationArguments args) throws Exception {
        clientBD2 = new NettyClient(host,bd2port,BdType.BD2,this);
        clientBD3 = new NettyClient(host,bd3port,BdType.BD3,this);
    }

    @Override
    public void onApplicationEvent(ContextClosedEvent event) {
        clientBD2.close();
        clientBD3.close();
    }
}
